/**
 * 以post方式访问服务器工具类
 * @author 黄广星
 * time:2014-6-7
 */
package com.taxsystem.tool;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class NetUtil {
	
	
	// 创建HttpClient对象
		public static HttpClient httpClient = new DefaultHttpClient();

		/**
		 * 以post方式访问服务器，返回字符串
		 * @param url
		 * @param pairList
		 * @return
		 * @throws InterruptedException
		 * @throws ExecutionException
		 */
		public static String getResultPost(final String url,final List<NameValuePair> pairList)
				throws InterruptedException, ExecutionException {
			FutureTask<String> task = new FutureTask<String>(
					new Callable<String>() {

						@Override
						public String call() throws Exception {
							HttpPost httpPost=new HttpPost(url);
							HttpEntity httpEntity=new UrlEncodedFormEntity(pairList);
							httpPost.setEntity(httpEntity);
							HttpClient httpClient=new DefaultHttpClient();
							HttpResponse httpResponse=httpClient.execute(httpPost);//有问题
							if (httpResponse.getStatusLine().getStatusCode() == 200) {
								// 获取服务器响应的字符串
								return EntityUtils.toString(httpResponse
										.getEntity());
							}
							return null;
						}
					});
			new Thread(task).start();
			return task.get();
		}
	
}